package com.tanhua.server.controller;

import com.tanhua.server.service.MovementsService;
import com.tanhua.server.service.VideoMQService;
import com.tanhua.server.service.VideoService;
import com.tanhua.server.vo.PageResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * @Author: ymm
 * @Description: 类内容说明
 * @Date: Created in 22:15 2021/1/8
 * @ModifiedBy:
 */
@RestController
@RequestMapping("smallVideos")
public class VideoController {
    @Autowired
    private VideoService videoService;

    @Autowired
    private VideoMQService videoMQService;
    /**
     * 小视频上传
     *
     * @param picFile
     * @param videoFile
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> saveVideo(@RequestParam(value = "videoThumbnail", required = false) MultipartFile picFile,
                                          @RequestParam(value = "videoFile", required = false) MultipartFile videoFile) {
        try {
            String id = this.videoService.saveVideo(picFile, videoFile);

            if (StringUtils.isNotEmpty(id)) {
                /**
                 * 发送消息
                 */
                this.videoMQService.videoMsg(id);
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 查询小视频列表
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping
    public ResponseEntity<PageResult> queryVideoList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                     @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        try {
            if (page <= 0) {
                page = 1;
            }
            PageResult pageResult = this.videoService.queryVideoList(page, pageSize);
            if (null != pageResult) {
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    @Autowired
    private MovementsService movementsService;

    /**
     * 小视频点赞
     *
     * @param videoId
     * @return
     */
    @PostMapping("/{id}/like")
    public ResponseEntity<Long> likeComment(@PathVariable(value = "id") String videoId) {
        try {
            Long likeCount = this.movementsService.likeComment(videoId);
            if (likeCount != null) {
                /**
                 * 发送消息
                 */
                this.videoMQService.likeVideoMsg(videoId);
                return ResponseEntity.ok(likeCount);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();    }


    /**
     * 小视频点赞取消
     *
     * @param videoId
     * @return
     */
    @PostMapping("/{id}/dislike")
    public ResponseEntity<Long> disLikeComment(@PathVariable(value = "id") String videoId) {
        try {
            Long likeCount = this.movementsService.disLikeComment(videoId);
            if (null != likeCount) {
                /**
                 * 发送消息
                 */
                this.videoMQService.disLikeVideoMsg(videoId);
                return ResponseEntity.ok(likeCount);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    /**
     * 视频中评论列表功能
     * @param videoId
     * @param page
     * @param pageSize
     * @return
     */
    @Autowired
    private CommentsController commentsController;
    @GetMapping("/{id}/comments")
    public ResponseEntity<PageResult> queryCommentList(@PathVariable(value = "id") String videoId,
                                                       @RequestParam(value = "page" ,defaultValue = "1") Integer page,
                                                       @RequestParam(value = "pagesize",defaultValue = "10") Integer pageSize){
        return this.commentsController.queryCommentsList(videoId,page,pageSize);
    }


    /**
     * 视频中评论发布
     * @param param
     * @param videoId
     * @return
     */
    @PostMapping("/{id}/comments")
    public ResponseEntity<Void> saveComments(@RequestBody Map<String,String> param, @PathVariable(value = "id") String videoId){
        param.put("movementId",videoId);
        return this.commentsController.saveComments(param);
    }


    @Autowired
    private MovementsController movementsController;
    /**
     * 视频中评论点赞
     * @param publishId
     * @return
     */
    @PostMapping("/{id}/comments/like")
    public ResponseEntity<Long> commentsLikeComment(@PathVariable(value = "id") String publishId) {
        return this.movementsController.likeComment(publishId);
    }


    /**
     * 视频中评论点赞取消
     * @param publishId
     * @return
     */
    @PostMapping("/{id}/comments/dislike")
    public ResponseEntity<Long> disCommentsLikeComment(@PathVariable(value = "id") String publishId) {
        return this.movementsController.disLikeComment(publishId);

    }


    /**
     * 关注用户
     * @param followUserId
     * @return
     */
    @PostMapping("/{uid}/userFocus")
    public ResponseEntity<Void> saveUserFocusComments(@PathVariable(value = "uid") Long followUserId){
        try {
            Boolean bool = this.videoService.followUser(followUserId);

            if(bool){
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    /**
     * 取消用户关注
     * @param followUserId
     * @return
     */
    @PostMapping("/{uid}/userUnFocus")
    public ResponseEntity<Void> saveUserUnFocusComments(@PathVariable(value = "uid") Long followUserId){
        try {
            Boolean bool = this.videoService.disFollowUser(followUserId);

            if(bool){
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


}
